package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.ActivityDAO;
import com.ecoolex.hotel.saas.entity.Activity;
import com.ecoolex.hotel.saas.entity.ActivityExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;

@Service
public class ActivityManager {

    @Autowired
    private ActivityDAO dao;

    @Autowired
    private ActivityStoreManager activityStoreManager;

    @Autowired
    private ActivityShowStoreManager activityShowStoreManager;

    public Activity get(Integer activityId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(activityId, Activity.NOT_DELETED);
    }

    public Pagination<Activity> query(String name, Page page) {
        ActivityExample example = new ActivityExample();
        ActivityExample.Criteria criteria = example.createCriteria();
        if (Check.notNull(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(Activity.NOT_DELETED);
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        example.orderBy(Activity.Column.weight.desc());
        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    /**
     * 用户端查询活动时根据配置端展示商户Id查询
     * @param storeId
     * @param page
     * @return
     */
    public Pagination<Activity> queryByShowStoreId(Integer storeId, Page page) {
        return Pagination.create(page, dao.countByShowStoreId(storeId), () -> dao.queryByShowStoreId(storeId, page));
    }

    private void verify(Activity record) {
        BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "活动名称不能为空");
        BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getTitle()), ResultCode.VERIFY.build(11), "页面标题不能为空");
        BizAssert.isTrue(Check.notNullAndGteZero(record.getWeight()), ResultCode.VERIFY.build(12), "权重不能为空");
    }

    public Activity add(Activity record, String createUser) {
        verify(record);
        record.setDeleted(Activity.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    public Activity edit(Activity record, String updateUser) {
        verify(record);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Activity.Column.excludes(Activity.Column.deleted, Activity.Column.createTime, Activity.Column.createUser));
        return record;
    }

    @Transactional
    public void delete(Activity record, String updateUser) {
        record.setDeleted(Activity.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Activity.Column.deleted, Activity.Column.updateUser, Activity.Column.updateTime);
        //删除内容关联的商户
        activityStoreManager.deleteByActivityId(record.getActivityId(), updateUser);
        //删除展示关联的商户
        activityShowStoreManager.deleteByActivityId(record.getActivityId(), updateUser);
    }
}